#include "myqgraphicsview.h"
#include <QDebug>

MyQGraphicsView::MyQGraphicsView(QWidget *parent):
    QGraphicsView(parent)
{
    m_scalnum = 1.0;    //把当前的缩放比例设置为1
    this->setCursor(Qt::OpenHandCursor);
    setMouseTracking(true);
    initMap();
}



void MyQGraphicsView::wheelEvent(QWheelEvent *event)
{
    if(event->angleDelta().y()>0)
    {
        this->scale(1.3, 1.3);
    }
    else
    {
        this->scale(1/1.3, 1/1.3);
    }
    QGraphicsView::wheelEvent(event);
}


void MyQGraphicsView::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {//左键拖拽
        this->setCursor(Qt::ClosedHandCursor);
        m_startpos = mapToScene(event->pos());
    }
    if (event->button() == Qt::RightButton)
    {//右键标点
        nowPoi = mapToScene(event->pos());
        QString xStr = QString::number(int(nowPoi.x()), 'f', 2); // x坐标，保留两位小数
        QString yStr = QString::number(int(nowPoi.y()), 'f', 2); // y坐标，保留两位小数
        initMap();  //重置地图
        drawPoint(nowPoi);  //标点
        drawLine(m_startpos,nowPoi);    //连线的测试代码
        drawInterest();     //景点描绘的测试代码
    }

    QGraphicsView::mousePressEvent(event);
}


void MyQGraphicsView::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton){//鼠标左键弹起进行视图移动
            this->setCursor(Qt::OpenHandCursor);
        m_endpos = mapToScene(event->pos());
        //获取滚动条当前位置
        double oposx = this->horizontalScrollBar()->value();
        double oposy = this->verticalScrollBar()->value();
        //计算鼠标移动的距离
        QPointF offset = m_endpos - m_startpos;
        //计算新的滚轮位置
        double nposx = oposx-offset.x()*m_scalnum;
        double nposy = oposy-offset.y()*m_scalnum;
        //设置新的滚轮位置
        this->horizontalScrollBar()->setValue(nposx);
        this->verticalScrollBar()->setValue(nposy);
        this->scene()->update();
    }
    QGraphicsView::mouseReleaseEvent(event);
}

void MyQGraphicsView::drawPoint(const QPointF &point) {
    // 创建一个QGraphicsEllipseItem来表示点
    QGraphicsEllipseItem *ellipseItem = new QGraphicsEllipseItem(point.x()-15, point.y()-15, 30, 30);
    // 设置颜色
    ellipseItem->setBrush(Qt::cyan);
    // 将点添加到场景中
    scene()->addItem(ellipseItem);
}

void MyQGraphicsView::initMap(){
    // 创建一个QGraphicsScene
    QGraphicsScene *scene = new QGraphicsScene(this);
    QPixmap pixmap(":/map.jpg");    //创建一个 `QPixmap` 对象，将需要显示的图片加载到其中
    scene->addPixmap(pixmap);    //将 `scene` 设置给 `QGraphicsView` 对象：
    setScene(scene);
}

void MyQGraphicsView::drawLine(const QPointF &start, const QPointF &end){
    // 创建一个QGraphicsLineItem来表示横线
    QGraphicsLineItem *lineItem = new QGraphicsLineItem(start.x(),start.y(), end.x(),end.y());
    // 设置颜色
    lineItem->setPen(QPen(Qt::red, 5));
    // 将横线添加到场景中
    scene()->addItem(lineItem);
}

QPointF MyQGraphicsView::getPoi(){
    return nowPoi;
}

void MyQGraphicsView::setLine(double x1,double y1,double x2,double y2){
    lineStart.setX(x1);
    lineStart.setY(y1);
    lineEnd.setX(x2);
    lineEnd.setY(y2);

}

void MyQGraphicsView::setInterest(vector<QPointF> data){

    interest.assign(data.begin(),data.end());
}

void MyQGraphicsView::drawInterest(){
    //测试
//    double x =10;double y =10;
//    for (int i = 0;i<10;i++) {
//        QPointF data(x,y);
//        interest.push_back(data);
//        x = 2*x;
//        y = 2* y;
//        qDebug()<<x<<"x"<<y;

//    }
    for (vector<QPointF>::iterator it = interest.begin(); it != interest.end(); ++it) {
        drawPoint(*it);
    }
}



